#!/bin/bash


target_dir="${1:-.}"

if [ ! -d "$target_dir" ]; then
    echo "错误：目录 $target_dir 不存在"
    exit 1
fi

cd "$target_dir" || {
    echo "错误：无法进入目录 $target_dir"
    exit 1
}

mkdir -p jpg_files png_files other_images

total=0

for file in *; do
   
    if [ -f "$file" ]; then
        
        ext=$(echo "$file" | awk -F. '{if(NF>1) print tolower($NF)}')
        
        case "$ext" in
            jpg|jpeg)
                mv "$file" jpg_files/
                total=$((total + 1))
                ;;
            png)
                mv "$file" png_files/
                total=$((total + 1))
                ;;
            gif|bmp|webp) 
                mv "$file" other_images/
                total=$((total + 1))
                ;;
        esac
    fi
done

echo "整理完成，共处理 $total 个图片文件"